//
//  VOHTopView.m
//  VoiceOfHeart
//
//  Created by 吉龙 on 16/8/16.
//  Copyright © 2016年 吉龙. All rights reserved.
//

#import "VOHTopView.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface VOHTopView ()
@end

@implementation VOHTopView

#pragma mark-----lazy





-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUpView];
    }
    return self;
}

-(void)setUpView{
    //头部轮播图
    UIScrollView *headerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)];
    headerView.contentSize = CGSizeMake(ScreenWidth * 6, 200);
    
    //    headerView.backgroundColor = [UIColor grayColor];
    headerView.pagingEnabled = YES;
    [self addSubview:headerView];
    self.headerView = headerView;
    self.headerView.contentOffset = CGPointMake(ScreenWidth, 0);
    
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(ScreenWidth - 100, CGRectGetHeight(self.headerView.frame) - 30, 90, 20)];
    [self addSubview:self.page];
    self.page.numberOfPages = 4;
    self.page.enabled = NO;
    
    [self addObserver:self forKeyPath:@"modelArr" options:NSKeyValueObservingOptionNew context:nil];

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    [self setPictureLoop];
    
//    [self removeObserver:self forKeyPath:@"modelArr"];

}


#pragma mark ------ 设置轮播图

-(void)setPictureLoop{
    for (int i = 0; i < self.modelArr.count; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * ScreenWidth, 0, ScreenWidth, self.headerView.bounds.size.height)];
                //添加手势
        VOHMVRecommendModel *model = self.modelArr[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.posterPic] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        
        imageView.userInteractionEnabled = YES;
        [self.headerView addSubview:imageView];
    }
    
}
-(void)tapAction:(UITapGestureRecognizer *)sender{
    UIImageView *imageView =(UIImageView *)sender.view;
    self.block(imageView);
}


@end
